package com.apes.autoparts.masterdata.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.autoparts.masterdata.model.EpcPartsName;
import com.apes.autoparts.masterdata.repository.EpcPartsNameRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.DateUtil;
import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @ClassName PartsNameService
 * @Description 配件名称
 * @Author pengyb
 * @Date 2019/11/8 18:06
 * @Version 1.0
 **/
@Service("epcPartsNameService")
public class EpcPartsNameService extends DomainService {
    @Autowired
    private EpcPartsNameRepository epcPartsNameRepository;

    @Autowired
    private EpcMasterDataManager epcMasterDataManager;

    private EpcPartsName save(SimpleRequest request) {
        EpcPartsName o = request.getO(EpcPartsName.class);
        o.setEntryDate(new Date());
        o.setTime(DateUtil.format(o.getEntryDate(), "yyyyMMdd.hhmmss.SSS"));
        o.setCreatorName(this.getSession(request).getUserName());
        //同步到新系统
        epcMasterDataManager.synEsb("autoparts.parts.name.sendSave", MapUtil.mapped(o));
        //同步到erp
        synPartsName(o, "S");
        return epcPartsNameRepository.save(o);
    }

    public EpcPartsName update(SimpleRequest request) {
        EpcPartsName partsName = request.getO(EpcPartsName.class);
        partsName.setEntryDate(new Date());
        partsName.setTime(DateUtil.format(partsName.getEntryDate(), "yyyyMMdd.hhmmss.SSS"));
        partsName.setCreatorName(this.getSession(request).getUserName());
        //同步到新系统
        epcMasterDataManager.synEsb("autoparts.parts.name.sendSave", MapUtil.mapped(partsName));
        //同步到erp
        synPartsName(partsName, "U");
        return epcPartsNameRepository.save(partsName);
    }

    private String delete(SimpleRequest request) {
        EpcPartsName o = request.getO(EpcPartsName.class);
        epcPartsNameRepository.delete(o);
        return "删除成功";
    }

    public void synPartsName(EpcPartsName partsName, String sign) {
        String method = "S".equals(sign) ? "mdmPjmcService.insert" : "mdmPjmcService.update";
        String TS01 = partsName.getTime();
        if ("U".equals(sign)) {
            //更新操作先查询
            TS01 = epcMasterDataManager.getVersion("mdmPjmcService.query", JSONObject.toJSONString(mapper("PJMCID", partsName.getId()))
            );
        }
        Map mapper = MapUtil.mapper("PJMCID", partsName.getId(), "PJMC", partsName.getName(), "PLID", partsName.getCategoryId(),
                "PLMC", partsName.getCategoryName(), "PYM", partsName.getPinYinMa(), "JJHSBJ", partsName.getRecycle(),
                "PPSPBJ", partsName.getMatchingProduct(), "PPYSBJ", partsName.getMatchingColor(), "DZYQJ", partsName.getElectronComponent(),"CZY",partsName.getCreatorName(), "TS01", TS01);

        epcMasterDataManager.synErp(method, JSONObject.toJSONString(mapper));
    }
}
